import wepy from 'wepy'
import storage from "@/utils/storage";

export default class CommonMixin extends wepy.mixin {
  data = {
    skin: wepy.$instance.globalData.skin
  }
  computed = {
    openId() {
      return wepy.$instance.globalData.openid;
    },
    shopId() {
      return wepy.$instance.globalData.globalConfig.SHOP_ID;
    }
  }
  onShow() {
    //检查版本
    this.checkVersion();
  }

  checkVersion() {
    if (typeof wx.getUpdateManager == 'undefined')
      return;
    const updateManager = wx.getUpdateManager();
    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      console.log('存在新版本:' + res.hasUpdate);
    });

    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate();
          }
        }
      });
    });

    updateManager.onUpdateFailed(function () {
      // 新的版本下载失败
    });
  };
  async onLoad(query) {
    console.info('[onLoad] query', query);
    let colorObject = {};
    if (wepy.$instance.globalData.navigationBarBackgroundColor) {
      Object.assign(colorObject, { backgroundColor: wepy.$instance.globalData.navigationBarBackgroundColor });
    }
    if (wepy.$instance.globalData.navigationBarFontColor) {
      Object.assign(colorObject, { frontColor: wepy.$instance.globalData.navigationBarFontColor });
    }
    wx.setNavigationBarColor(colorObject);

    //获取分享者openid 并存储到本地
    if (query.share_openid) {
      storage.save('share_openid', query.share_openid);
      wepy.$instance.globalData.share_openid = query.share_openid;
    }
    else {
      //加载分享者openid
      wepy.$instance.globalData.share_openid = await storage.get('share_openid');
    }
    if (query.scene) {
      let scene = decodeURIComponent(query.scene);
      if (scene && scene != '') {
        storage.save('share_openid', scene);
        wepy.$instance.globalData.share_openid = scene;
      }
    }
  }
}